﻿/*********************************************
*Create By：Paine Zeng
*Create Date: 2023/07/27 13:18:02
*Description:
*Email: tozeng@vip.qq.com
*Blog:  https://blog.csdn.net/zengqh0314
*Gitee: https://gitee.com/painezeng
**********************************************/

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.IO;

namespace SolidWorks.Base
{
    /// <summary>
    /// Model 操作
    /// </summary>
    public static class ModelHelper
    {
        /// <summary>
        /// 零件后缀名
        /// </summary>
        public const string Ext_Part = ".sldprt";

        /// <summary>
        /// 装配后缀名
        /// </summary>
        public const string Ext_Assembly = ".sldasm";

        /// <summary>
        /// 工程图后缀名
        /// </summary>
        public const string Ext_Drawing = ".slddrw";

        /// <summary>
        /// 打开SolidWorks文件
        /// </summary>
        /// <param name="swApp"></param>
        /// <param name="filePath"></param>
        /// <param name="openErr"></param>
        /// <param name="openWarn"></param>
        /// <param name="activateErr"></param>
        /// <param name="visible"></param>
        /// <param name="activate"></param>
        /// <returns></returns>
        public static ModelDoc2 OpenExistFile(this SldWorks swApp, string filePath, out int openErr, out int openWarn, out int activateErr, bool visible = true, bool activate = true)
        {
            openErr = 0;
            openWarn = 0;
            activateErr = 0;

            var docType = GetSolidWorksDocType(filePath);

            if (docType == swDocumentTypes_e.swDocNONE)
            {
                return swApp.LoadFile4(filePath, "r", null, ref openErr);
            }

            if (visible == false)
                swApp.DocumentVisible(false, (int)docType);

            var swModel = swApp.OpenDoc6(filePath, (int)docType, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, null, openErr, openWarn);

            if (swModel == null)
            {
                if (visible == false)
                    swApp.DocumentVisible(true, (int)docType);
                return null;
            }

            var activeDoc = (ModelDoc2)swApp.ActiveDoc;

            if (activate && swApp.IsSame(activeDoc, swModel) == (int)swObjectEquality.swObjectNotSame)
                swModel = (ModelDoc2)swApp.ActivateDoc3(filePath, false, (int)swRebuildOnActivation_e.swRebuildActiveDoc, activateErr);

            if (visible == false)
                swApp.DocumentVisible(true, (int)docType);

            return swModel;
        }

        /// <summary>
        /// 创建并返回新文档
        /// </summary>
        /// <param name="swApp"></param>
        /// <param name="docType">零件=1 装配=2 工程图=3</param>
        /// <param name="templatePath">模板路径，可为空</param>
        /// <returns></returns>

        public static ModelDoc2 CreateModel(this SldWorks swApp, int docType, string templatePath = null)
        {
            if (templatePath == null)
            {
                switch (docType)
                {
                    case (int)swDocumentTypes_e.swDocPART:
                        {
                            templatePath = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
                            break;
                        }

                    case (int)swDocumentTypes_e.swDocASSEMBLY:
                        {
                            templatePath = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);
                            break;
                        }

                    case (int)swDocumentTypes_e.swDocDRAWING:
                        {
                            templatePath = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
                            break;
                        }
                }
            }

            return (ModelDoc2)swApp.NewDocument(templatePath, 0, 0, 0);
        }

        /// <summary>
        /// 返回文件类型
        /// </summary>
        /// <param name="filePath">文件完整路径</param>
        /// <returns></returns>
        public static swDocumentTypes_e GetSolidWorksDocType(string filePath)
        {
            if (filePath == null)
                return swDocumentTypes_e.swDocNONE;

            switch (Path.GetExtension(filePath).ToLower())
            {
                case Ext_Part:
                    {
                        return swDocumentTypes_e.swDocPART;
                    }

                case Ext_Assembly:
                    {
                        return swDocumentTypes_e.swDocASSEMBLY;
                    }

                case Ext_Drawing:
                    {
                        return swDocumentTypes_e.swDocDRAWING;
                    }

                default:
                    {
                        return swDocumentTypes_e.swDocNONE;
                    }
            }
        }
    }
}